package com.auto.survey.activity.adjustment.adjustment_high

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.dao.AdjustmentRepository
import com.auto.survey.activity.config.ViewPagerAdapter
import com.auto.survey.base.BaseActivity
import com.auto.survey.base.ICreatView
import com.auto.survey.log.AppLogger
import com.auto.survey.util.Helper
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.coroutines.launch

class HighDetailActivity : BaseActivity(){

    companion object {
        val KEY_INTENT_PROJECT_ID = "intent_id"
        val KEY_INTENT_PROJECT_NAME = "intent_name"
    }

    private val TAG :String = HighDetailActivity::class.java.simpleName
    private val viewModel:HighAdjusMentModle by viewModels()
    private val repository = AdjustmentRepository()

    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout

    private var project_id = -1
    override fun initView() {

        project_id = intent.getIntExtra(KEY_INTENT_PROJECT_ID, -1)
        val project_name = intent.getStringExtra(KEY_INTENT_PROJECT_NAME)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.blue)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.blue)
        mComment_title?.setTitle("项目详情-$project_name")

        mComment_title?.setRightTitle("开始平差")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{

        })

        viewPager = findViewById(R.id.project_viewPager)
        tabLayout = findViewById(R.id.project_tabLayout)

        val viewCreators: List<ICreatView> = listOf(
            ProjectDiscribView(viewModel, project_id, this@HighDetailActivity),
            PointView(viewModel, project_id, this@HighDetailActivity),
            ObservationView(viewModel,project_id, this@HighDetailActivity),
            AdjustMentResultView(viewModel,project_id, this@HighDetailActivity)
        )
        val views = mutableListOf<View>()
        try {
            viewCreators.forEach { creator ->
                creator.creatView(viewPager.context, viewPager)
                views.add(creator.getView())
            }
        } catch (e: Exception) {
            AppLogger.getInstance(this@HighDetailActivity).e(TAG, "创建视图失败: ${e.message}", e)
            // 可添加默认视图或空状态处理
        }

        val adapter = ViewPagerAdapter()
        adapter.setViews(views)
        // 设置适配器
        viewPager.adapter = adapter

        // 配置 TabLayout
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            if (position < viewCreators.size) {
                tab.text = viewCreators[position].getTitle()
            } else {
                tab.text = "未知页面" // 处理越界情况
            }
        }.attach()

        lifecycleScope.launch {
            viewModel.project.observe(this@HighDetailActivity){

            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getProjectDetail(repository,project_id)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_project_detail
    }
}